
Ext.onReady(function(){
    var current_order_id; //当前选择的order的id
    var current_ticket_id; //当前选择的ticket的id
    var current_order_status; //当前order的状态
    var current_ticket_status;
    var ordersrow;
    var ticketsrow;
    var current_order_no;
    
    setTimeout("refreshDS()", 300000);
    refreshDS = function(){
        var time = parseInt(refreshTime.getValue()) * 60000;
        orderds.reload();
        ticketds.baseParams.id = "";
        ticketds.reload();
        setTimeout("refreshDS()", time);
    };
    
    var orderNoField = new Ext.form.TextField({
        fieldLabel: '订单编号',
        name: 'order_no',
        allowBlank: true,
        anchor: '99%'
    });
    var pnrField = new Ext.form.TextField({
        fieldLabel: 'PNR',
        name: 'pnr',
        allowBlank: true,
        anchor: '99%'
    });
    var linkManField = new Ext.form.TextField({
        fieldLabel: '联系人',
        name: 'man',
        allowBlank: true,
        anchor: '99%'
    });
    var linkTelField = new Ext.form.TextField({
        fieldLabel: '联系电话',
        name: 'tel',
        allowBlank: true,
        anchor: '99%'
    });
    var orderToday = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'order_date',
        boxLabel: '今天的订单',
        checked: true,
        inputValue: 'today'
    });
    var orderBefore = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'order_date',
        boxLabel: '之前的订单',
        checked: false,
        inputValue: 'before'
    });
    var oStatus = new Ext.form.ComboBox({
        store: new Ext.data.Store({
            reader: new Ext.data.ArrayReader({}, ['value']),
            data: [['未付款'], [ORDER_STATUS_YFK], [ORDER_STATUS_YQX], [ORDER_STATUS_YZF], [ORDER_STATUS_GQD]]
        }),
        name: 't_status',
        fieldLabel: '订单状态',
        resiable: true,
        typeAhead: true,
        width: 100,
        handleHeight: 100,
        displayField: 'value',
        valueField: 'value',
        nameField: 'oStatus',
        selectOnFocus: true,
        mode: 'local',
        selectOnFocus: true,
        triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
        readOnly: true,
        emptyText: '所有类型...',
        allowBlank: false,
        value: '未付款'
    });
    var refreshTime = new Ext.form.ComboBox({
        store: new Ext.data.Store({
            reader: new Ext.data.ArrayReader({}, ['value']),
            data: [['1'], ['5'], ['10'], ['30']]
        }),
        name: 're_time',
        fieldLabel: '刷新间隔(分)',
        resiable: true,
        typeAhead: true,
        width: 60,
        handleHeight: 100,
        displayField: 'value',
        valueField: 'value',
        nameField: 're_time',
        selectOnFocus: true,
        mode: 'local',
        selectOnFocus: true,
        triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
        readOnly: true,
        allowBlank: true,
        value: '5'
    });
    
    //提交按钮
    var submitButton = new Ext.Button({
        text: '查询',
        handler: function(){
            if (formPanel.form.isValid()) {
                var status = oStatus.getValue();
                var order_date = "today";
                if (orderBefore.checked) {
                    order_date = "before";
                }
                var no = orderNoField.getValue();
                var pnr = pnrField.getValue();
                var man = linkManField.getValue();
                var tel = linkTelField.getValue();
                
                //alert(status+"-"+order_date+"-"+no+"-"+pnr+"-"+man+"-"+tel);
                orderds.baseParams.status = status;
                orderds.baseParams.order_date = order_date;
                orderds.baseParams.order_no = no;
                orderds.baseParams.pnr = pnr;
                orderds.baseParams.link_tel = tel;
                orderds.baseParams.link_man = man;
                orderds.reload();
                
                ticketds.baseParams.id = "";
                ticketds.reload();
            }
            else {
                Ext.MessageBox.alert('提示信息', '请将必填项信息录入后再提交.');
            }
        }
    })
    
    var formPanel = new Ext.form.FormPanel({
        //baseCls: 'x-plain',
        labelWidth: 75,
        width: '100%',
        frame: true,
        title: '审核收款',
        labelAlign: 'right',
        items: [{
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [orderNoField]
            }, {
                columnWidth: .15,
                layout: 'form',
                border: false,
                items: [pnrField]
            }, {
                columnWidth: .15,
                layout: 'form',
                border: false,
                items: [linkManField]
            }, {
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [linkTelField]
            }]
        }, {
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .21,
                layout: 'form',
                border: false,
                items: [oStatus]
            }, {
                columnWidth: .1,
                layout: 'form',
                border: false,
                items: [orderToday]
            }, {
                columnWidth: .12,
                layout: 'form',
                border: false,
                items: [orderBefore]
            }, {
                columnWidth: .18,
                layout: 'form',
                border: false,
                items: [refreshTime]
            }, {
                columnWidth: .3,
                layout: 'form',
                border: false,
                items: [submitButton]
            }]
        }]
    });
    
    formPanel.render('cash_check_form');
    
    //############## 订单Grid ##############
    var ordercm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: '订单编号',
        dataIndex: 'order_no',
        width: 120
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '客户名称',
        dataIndex: 'customer',
        width: 70
    }, {
        header: '订单状态',
        dataIndex: 'status',
        width: 60
    }, {
        header: '总金额',
        dataIndex: 'price_sum',
        width: 60
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 60
    }, {
        header: '联系人',
        dataIndex: 'link_man',
        width: 60
    }, {
        header: '联系电话',
        dataIndex: 'link_tel',
        width: 90
    }, {
        header: '送票地址',
        dataIndex: 'send_address',
        width: 120
    }, {
        header: '送票要求',
        dataIndex: 'send_request',
        width: 120
    }, {
        header: '送票员',
        dataIndex: 'sender',
        width: 60
    }, {
        header: '客户要求',
        dataIndex: 'order_request',
        width: 120
    }, {
        header: '预定员',
        dataIndex: 'creator',
        width: 60
    }, {
        header: '预定时间',
        dataIndex: 'created_at',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '出票员',
        dataIndex: 'ticket_outer',
        width: 60
    }, {
        header: '出票时间',
        dataIndex: 'ticket_out_date',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '收款员',
        dataIndex: 'cash_checker',
        width: 60
    }, {
        header: '收款时间',
        dataIndex: 'cash_get_date',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '收款方式',
        dataIndex: 'pay_type',
        width: 60
    }, {
        header: '收款金额',
        dataIndex: 'receive_cash',
        width: 60
    }]);
    ordercm.defaultSortable = true;
    var orderds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getCashCheckList'
        }),
        reader: orderReader
    });
    
    var ordergridpanel = new Ext.grid.GridPanel({
        el: 'ordersgrid',
        ds: orderds,
        cm: ordercm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 330,
        title: '订单信息'
    });
    
    ordergridpanel.render();
    orderds.load();
    
    //双击订单查看机票详细
    ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        current_order_no = ordersrow.get("order_no");
        ticketds.baseParams.id = current_order_id;
        ticketds.reload();
    });
    //订单右键菜单
    ordergridpanel.on('rowcontextmenu', function(customergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        current_order_no = ordersrow.get("order_no");
        e.preventDefault();
        rightClick.showAt(e.getXY());
    });
    
    //############ 订单-右键菜单 ##########
    var rightClick = new Ext.menu.Menu({
        items: [{
            handler: rightMenu0Fn,
            text: '【审核收款】'
        }, {
            handler: rightMenu5Fn,
            text: '【作废】'
        }, "-", {
            handler: rightMenu1Fn,
            text: '【修改订单】'
        }, {
            handler: rightMenu4Fn,
            text: '【取消订单】'
        }, {
            handler: rightMenu2Fn,
            text: '【提取PNR】'
        }, {
            handler: rightMenu3Fn,
            text: '【查看客户】'
        }]
    });
    function rightMenu0Fn(){
        if (current_order_status == ORDER_STATUS_YCP) {
            var price_sum = ordersrow.get("price_sum");
            CheckCash(price_sum);
        }
        else {
            Ext.MessageBox.alert('操作提示', '只有已出票状态的订单才可以收款');
        }
    }
    function rightMenu1Fn(){
        ModOrder(orderds, current_order_id, ticketds);
    }
    function rightMenu2Fn(){
        showPnrInfo(current_order_id);
    }
    function rightMenu3Fn(){
        var customer_id = ordersrow.get("customer_id");
        showCustomerInfo(customer_id);
    }
    function rightMenu4Fn(){
        var current_order_no = ordersrow.get("order_no");
        cancelOrder(current_order_status, current_order_id, current_order_no, orderds, ticketds);
    }
    function rightMenu5Fn(){
        if (current_order_status == ORDER_STATUS_YZF) {
            Ext.MessageBox.alert('操作提示', '该订单已经作废');
        }
        else if (current_order_status == ORDER_STATUS_YQX) {
            Ext.MessageBox.alert('操作提示', '该订单已经取消');
        }
        else {
            setTicketZF();
        }
    }
    //### 机票Grid ###
    var ticketcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '旅客姓名',
        dataIndex: 'passenger_name',
        width: 70
    }, {
        header: '证件号码',
        dataIndex: 'card',
        width: 140
    }, {
        header: '票号',
        dataIndex: 'ticket_no',
        width: 110
    }, {
        header: '状态',
        dataIndex: 'status_type',
        width: 50
    }, {
        header: '出票状态',
        dataIndex: 'status',
        width: 60
    }, {
        header: '航班号',
        dataIndex: 'air_no',
        width: 60
    }, {
        header: '航程',
        dataIndex: 'fromto',
        width: 60
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 60
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 40
    }, {
        header: '机票价格',
        dataIndex: 'price',
        width: 60
    }, {
        header: '机场费税',
        dataIndex: 'airdrome_tax',
        width: 60
    }, {
        header: '附加费',
        dataIndex: 'fuel_tax',
        width: 60
    }, {
        header: 'F',
        dataIndex: 'price_F',
        width: 40
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 60
    }, {
        header: '保险F',
        dataIndex: 'insurance_F',
        width: 50
    }, {
        header: '其他',
        dataIndex: 'other',
        width: 50
    }, {
        header: '积分',
        dataIndex: 'point',
        width: 50
    }, {
        header: '合计',
        dataIndex: 'total_price',
        width: 60
    
    }]);
    ticketcm.defaultSortable = true;
    var ticketProxy = new Ext.data.HttpProxy({
        url: '/orders/getTicketsByOrderID?showCancelTicket=no'
    });
    
    var ticketds = new Ext.data.Store({
        proxy: ticketProxy,
        reader: ticketReader
    });
    
    var ticketgridpanel = new Ext.grid.GridPanel({
        el: 'ticketsgrid',
        ds: ticketds,
        cm: ticketcm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 180,
        title: '航班信息'
    });
    
    ticketgridpanel.render();
    ticketds.load();
    
    //############ 票-右键菜单 ##########
    function ticketRightMenu1(){
        ModTicket(ticketds, current_ticket_id, orderds);
    }
    function ticketRightMenu2(){
        var current_passenger_name = ticketsrow.get("passenger_name");
        cancelTicket(current_ticket_status, current_ticket_id, current_passenger_name, ticketds);
    }
    var ticketRightClick = new Ext.menu.Menu({
        items: [{
            handler: ticketRightMenu1,
            text: '【编辑修改】'
        }, {
            handler: ticketRightMenu2,
            text: '【取消乘客】'
        }]
    });
    ticketgridpanel.on('rowcontextmenu', function(ticketgridpanel, rowIndex, e){
        var current_ticket_selected = ticketds.data.items[rowIndex].id;
        ticketsrow = ticketds.getById(current_ticket_selected);//ds为绑定到grid的数据源
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        e.preventDefault();
        ticketRightClick.showAt(e.getXY());
    });
    
    //审核收款
    function CheckCash(price_sum){
        var should = new Ext.form.TextField({
            fieldLabel: '应收金额',
            name: 'should',
            readOnly: true,
            width: 100,
            value: price_sum
        });
        var receive = new Ext.form.NumberField({
            fieldLabel: '实收金额',
            name: 'receive',
            allowBlank: false,
            blankText: "实收金额不能为空",
            value: price_sum,
            width: 100
        });
        var payType = new Ext.form.ComboBox({
            store: new Ext.data.Store({
                reader: new Ext.data.ArrayReader({}, ['value']),
                data: [[PAY_TYPE_XJ], [PAY_TYPE_SK], [PAY_TYPE_QT], [PAY_TYPE_ZP], [PAY_TYPE_ZZ], [PAY_TYPE_JF]]
            }),
            name: 'payType',
            fieldLabel: '支付方式',
            resiable: true,
            typeAhead: true,
            width: 100,
            handleHeight: 100,
            displayField: 'value',
            valueField: 'value',
            nameField: 'payType',
            selectOnFocus: true,
            mode: 'local',
            selectOnFocus: true,
            triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
            readOnly: true,
            emptyText: '选择类型...',
            allowBlank: false,
            blankText: "支付方式必须选择",
            value: PAY_TYPE_XJ
        });
        var sender = new Ext.form.ComboBox({
            store: new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({
                    url: '/users/getSendersComboBox'
                }),
                reader: new Ext.data.JsonReader({
                    root: 'root'
                }, ['id', 'user_name'])
            }),
            fieldLabel: '送票员',
            name: 'sender',
            loadingText: '正在加载...',
            displayField: 'user_name',
            valueField: 'id',
            mode: 'remote',
            //hideTrigger: true,
            triggerAction: 'all',
            hiddenName: 'sender',//如果不想提交displayField，则在这儿指定以提交ID的值．
            //hiddenId:'sender',
            //emptyText: '请选择...',
            editable: false,
            allowBlank: true,
            //forceSelection: false,
            width: 100,
            //maxHeight: 150,
            tabIndex: 4
        });
        var remark = new Ext.form.TextArea({
            fieldLabel: '备注',
            name: 'remark',
            height: 40,
            allowBlank: true,
            anchor: '99%'
        });
        
        var cash_formPanel = new Ext.form.FormPanel({
            //baseCls: 'x-plain',
            labelWidth: 55,
            labelAlign: 'right',
            frame: true,
            //title: '订票',
            url: "/orders/saveCashChecker?id=" + current_order_id,
            items: [should, receive, payType, sender, remark]
        });
        
        //收款审核窗口
        var checkWindow = new Ext.Window({
            title: '审核收款',
            width: 300,
            height: 250,
            layout: 'fit',
            plain: true,
            bodyStyle: 'padding:5px;',
            buttonAlign: 'center',
            items: cash_formPanel,
            buttons: [{
                text: '审核收款',
                handler: function(){
                    if (cash_formPanel.form.isValid()) {
                        if (receive.getValue() > 0) {
                            cash_formPanel.form.submit({
                                waitMsg: 'In processing',
                                success: function(form, action){
                                    var info = "";
                                    if (action && action.result) {
                                        info = action.result.info;
                                    }
                                    else {
                                        info = '操作失败,具体失败原因请查询日志文件';
                                    }
                                    if (info.indexOf("成功") >= 0) {
                                        checkWindow.hide();
                                        orderds.baseParams.status = "已付款";
                                        orderds.reload();
                                        ticketds.baseParams.id = "";
                                        ticketds.reload();
                                        Ext.MessageBox.alert('操作成功', '订单' + current_order_no + '付款成功');
                                    }
                                    else {
                                        Ext.MessageBox.alert('操作失败', info);
                                    }
                                },
                                failure: function(form, action){
                                    var info = "";
                                    if (action && action.result) {
                                        info = action.result.info;
                                    }
                                    else {
                                        info = '操作失败,具体失败原因请查询日志文件';
                                    }
                                    Ext.MessageBox.alert('操作信息', info);
                                }
                            });
                        }
                        else {
                            Ext.MessageBox.alert('操作提示', '实收金额不能为0');
                        }
                    }
                }
            }, {
                text: '取消',
                handler: function(){
                    checkWindow.hide();
                }
            }]
        });
        checkWindow.show();
    }
    function setTicketZF(){
        var receive = new Ext.form.NumberField({
            fieldLabel: '工本费',
            name: 'receive',
            allowBlank: false,
            blankText: "工本费不能为空",
            value: 10,
            width: 100
        });
        var payType = new Ext.form.ComboBox({
            store: new Ext.data.Store({
                reader: new Ext.data.ArrayReader({}, ['value']),
                data: [[PAY_TYPE_XJ], [PAY_TYPE_SK], [PAY_TYPE_QT], [PAY_TYPE_ZP], [PAY_TYPE_ZZ], [PAY_TYPE_JF]]
            }),
            name: 'payType',
            fieldLabel: '支付方式',
            resiable: true,
            typeAhead: true,
            width: 100,
            handleHeight: 100,
            displayField: 'value',
            valueField: 'value',
            nameField: 'payType',
            selectOnFocus: true,
            mode: 'local',
            selectOnFocus: true,
            triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
            readOnly: true,
            emptyText: '选择类型...',
            allowBlank: false,
            blankText: "支付方式必须选择",
            value: PAY_TYPE_XJ
        });
        var remark = new Ext.form.TextArea({
            fieldLabel: '作废原因',
            name: 'remark',
            height: 40,
            allowBlank: true,
            anchor: '99%'
        });
        
        var zf_formPanel = new Ext.form.FormPanel({
            //baseCls: 'x-plain',
            labelWidth: 55,
            labelAlign: 'right',
            frame: true,
            //title: '订票',
            url: "/orders/saveCashChecker?id=" + current_order_id + "&type=zf",
            items: [receive, payType, remark]
        });
        
        //作废窗口
        var zfWindow = new Ext.Window({
            title: '作废',
            width: 250,
            height: 200,
            layout: 'fit',
            plain: true,
            bodyStyle: 'padding:5px;',
            buttonAlign: 'center',
            items: zf_formPanel,
            buttons: [{
                text: '确定',
                handler: function(){
                    if (zf_formPanel.form.isValid()) {
                        zf_formPanel.form.submit({
                            waitMsg: 'In processing',
                            success: function(form, action){
                                var info = "";
                                if (action && action.result) {
                                    info = action.result.info;
                                }
                                else {
                                    info = '操作失败,具体失败原因请查询日志文件';
                                }
                                if (info.indexOf("成功") >= 0) {
                                    zfWindow.hide();
                                    orderds.baseParams.status = "已作废";
                                    orderds.reload();
                                    ticketds.baseParams.id = "";
                                    ticketds.reload();
                                    Ext.MessageBox.alert('操作成功', '订单' + current_order_no + '作废成功');
                                }
                                else {
                                    Ext.MessageBox.alert('操作失败', info);
                                }
                            },
                            failure: function(form, action){
                                var info = "";
                                if (action && action.result) {
                                    info = action.result.info;
                                }
                                else {
                                    info = '操作失败,具体失败原因请查询日志文件';
                                }
                                Ext.MessageBox.alert('操作失败', info);
                            }
                        });
                    }
                }
            }, {
                text: '取消',
                handler: function(){
                    zfWindow.hide();
                }
            }]
        });
        zfWindow.show();
    }
    
    
    
})
